package spring.boot.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import spring.boot.admin.model.SysUser;
import spring.boot.admin.service.SysRoleService;
import spring.boot.admin.service.SysUserRoleService;
import spring.boot.admin.service.SysUserService;

import java.util.List;
import java.util.Map;

/**
 * 用户管理  控制器
 */
@Controller
@RequestMapping("/sys/user")
public class SysUserController {


    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysRoleService sysRoleService;


    /**
     * 跳转到用户管理页面
     */
    @GetMapping("to/list")
    @PreAuthorize("hasRole('SYS_USER_LIST')")
    public String toList() {
        return "sys/user/list";
    }


    /**
     * 分页查询用户列表
     */
    @PostMapping("list")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_USER_LIST')")
    public IPage<SysUser> list(Integer page, Integer rows, String realName) {
        return sysUserService.pageList(page, rows, realName);
    }


    /**
     * 新增用户
     */
    @PostMapping("insert")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_USER_ADD')")
    public String insert(@RequestParam(required = false) String username,
                         @RequestParam(required = false) Integer sex,
                         @RequestParam(required = false) Integer age,
                         @RequestParam(required = false) String realName) {
        if (username == null || sex == null || age == null || realName == null) {
            return "-1";
        }
        return sysUserService.insert(username, age, sex, realName);
    }


    /**
     * 根据主键删除用户
     */
    @PostMapping("deleteById")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_USER_DELETE')")
    public String deleteById(@RequestParam(required = false) Long id) {
        if (id == null) {
            return "-1";
        }
        return sysUserService.deleteById(id);
    }


    /**
     * 根据主键更新用户信息
     */
    @PostMapping("update")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_USER_UPDATE')")
    public String update(@RequestParam(required = false) Long id,
                         @RequestParam(required = false) String username,
                         @RequestParam(required = false) Integer sex,
                         @RequestParam(required = false) Integer age,
                         @RequestParam(required = false) String realName) {
        if (id == null || username == null || sex == null || age == null || realName == null) {
            return "-1";
        }
        return sysUserService.updateUserById(id, username, age, sex, realName);
    }


    /**
     * 根据主键修改用户是否有效字段
     */
    @PostMapping("updateEnableById")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_USER_ENABLE_UPDATE')")
    public String updateEnableById(@RequestParam(required = false) Long id, @RequestParam(required = false) Integer enable) {
        if (id == null || enable == null) {
            return "-1";
        }
        return sysUserService.updateEnableById(id, enable);
    }


    /**
     * 修改个人登录密码
     */
    @PostMapping("updatePassword")
    @ResponseBody
    @PreAuthorize("true")
    public String updatePassword(String oldPassword, String newPassword) {
        if (oldPassword == null || newPassword == null) {
            return "-1";
        }
        return sysUserService.updatePassword(oldPassword, newPassword);
    }


    /**
     * 根据用户id查询用户拥有的所有角色
     */
    @PostMapping("selectRolesByUserId")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_USER_ROLE_UPDATE')")
    public List<Long> selectRolesByUserId(@RequestParam(required = false) Long userId) {
        if (userId == null) {
            return null;
        }
        return sysUserRoleService.selectRoleIdsByUserId(userId);
    }


    /**
     * 查询所有有效的角色列表
     */
    @PostMapping("userRoles")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_USER_ROLE_UPDATE')")
    public List<Map> userRoles() {
        return sysRoleService.selectUserRoleList();
    }


    /**
     * 更新用户的角色
     */
    @PostMapping("updateUserRole")
    @ResponseBody
    @PreAuthorize("hasRole('SYS_USER_ROLE_UPDATE')")
    public String updateUserRole(@RequestParam(required = false) Long userId,
                                 @RequestParam(required = false) Long[] roleIds) {
        if (userId == null || roleIds == null) {
            return "-1";
        }
        return sysUserRoleService.updateUserRole(userId, roleIds);
    }

}
